<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'奖励明细'),
	array('op'=>'listday','title'=>'每日奖励统计'),
	array('op'=>'listmonth','title'=>'每月奖励统计'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>4);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$order=pdo_get('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']));
			$item['level']=$item['type']?:$item['level'];
			if($item['is_sale']){
				$item['level']='sale'.$item['level'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array(
			'tip'=>'只有是代理商的，才能分配到奖励！有些代理商就算有下级，如果被关闭了也是不能获得奖励的，请知悉！
			</br>1、奖励太低了，奖励低于0.01元是不会计算显示的；会被忽略掉；
			',
			'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'经销商','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'type','title'=>'奖励类型','type'=>'radio','options'=>'commission_money_types'),
			array('name'=>'money','title'=>'额度','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'奖励状态','type'=>'radio','options'=>'commission_status'),
			//array('name'=>'complete_time','title'=>'订单完成时间','type'=>'time'),
			//array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		),'action'=>array(
			//array('type'=>'modal','op'=>'change','title'=>'修改奖励'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'奖励状态','options'=>'commission_status'),
			array('name'=>'type','width'=>15,'title'=>'奖励类型','type'=>'radio','options'=>'commission_money_types'),
		),
	);
	$return['htmls']=$htmls;
	$return['commission_status']=$this->commission_status;
	$return['commission_money_types']=$this->commission_money_types;
	exi($return);
}
if($_RQ['op']=='listday'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>4);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_newcar_commission',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>4);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$m=$i+1;
			$endtime=strtotime($y.'-'.$m);
		}
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_newcar_commission',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'月份','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('commission',array('money'=>$post['money']),array('id'=>$id));
		exi('修改奖励成功！','','list');
	}
	$item=$this->get('commission',array('id'=>intval($_RQ['id'])),array('id','money'));
	if(!$item){
		exi('指定奖励不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改奖励',
		'edit'=>array(
			array('type'=>'string','name'=>'money','title'=>'设置奖励','help'=>'奖励异常时操作'),
		),
	);
	exi($return);
}
if($_RQ['op']=='fee3check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->model('commission')->fee3check($post,true);
		exi('产生成功！','','list');
	}
	$item=$this->get('commission',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定奖励不存在！','error');
	}
	$msg=$this->model('commission')->fee3check($item);
	$item['msg']=$msg;
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'平级描述',
		'edit'=>array(
			array('type'=>'copy','name'=>'msg','title'=>'平级描述'),
		),
	);
	exi($return);
}
if($_RQ['op']=='detail'){
	$item=$this->get('commission',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])),array('id','remark'));
	if(!$item){
		exi('指定奖励不存在！','error');
	}
	$item['remark']=$this->model('commission')->commission_remark($item['remark']);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'奖励明细',
		'look'=>array(
			array('type'=>'text','name'=>'remark','title'=>'奖励明细'),
		),
	);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>4);
    if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$order=$this->pdo_getall('commission',$params);
	if(count($order)>5000){
		exi('数量大于5000，请修改搜索条件！','error');
	}
	foreach($order as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['nickname']=$item['member']['nickname'];
			$item['leveltitle']=$this->commission_money_types2[$item['type']];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['complete_time']=date('Y-m-d H:i:s',$item['complete_time']);
			$item['statustitle']=$this->commission_status[$item['status']];
	}
	$header=array(
		'mid'=>'经销商ID',
		'nickname'=>'经销商',
		'ordersn'=>'订单号',
		'leveltitle'=>'奖励类型',
		'money'=>'奖励',
		'createtime'=>'创建时间',
		'statustitle'=>'奖励状态',
		'complete_time'=>'订单完成时间',
		'balancesn'=>'结算单号',
	);
	cfc('tools')->export2excel($header,$order,'奖励列表');
}